package hotel.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import hotel.dao.DaoManager;
import hotel.dao.UserDao;
import hotel.dao.base.ConnectionBehavior;
import hotel.dao.base.DaoCommand;
import hotel.entity.UserEntity;
import hotel.model.IUser;
import hotel.model.User;
import hotel.service.UserService;

public class UserServiceImpl implements UserService 
{

	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
	private static final String USERSERVICEIMPL_ERROR = "Error in UserServiceImpl:";
	
	private DaoManager daoManager;

	public UserServiceImpl(DaoManager daoManager) 
	{
		this.daoManager = daoManager;
	}

	protected DaoManager getDaoManager() 
	{
		return daoManager;
	}
	
	@Override
	public IUser getUserById(final int id)
	{
		return (IUser)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final UserDao userDao = manager.getDao(UserDao.class);
					    	UserEntity userEntity = null;
					    	try
					    	{
					    		userEntity = userDao.findById(id);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(USERSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	IUser user = null;
					    	
					    	user = convertToModel(userEntity);
			  		
					    	return user;
						}
					}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IUser getUserByUsername(final String username)
	{
		return (IUser)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final UserDao userDao = manager.getDao(UserDao.class);
					    	UserEntity userEntity = null;
					    	try
					    	{
					    		userEntity = userDao.findByUsername(username);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(USERSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	IUser user = null;
					    	if (userEntity != null)
					    	{
					    		user = convertToModel(userEntity);
					    	}
					    	return user;
						}
					}, ConnectionBehavior.CLOSE);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IUser> getUsers() 
	{
		return (List<IUser>)
		getDaoManager().command(new DaoCommand()
			{
				public Object execute(final DaoManager manager) 
				{
					final UserDao userDao = manager.getDao(UserDao.class);
			    	List<UserEntity> listUserEntity = null;
			    	try
			    	{
			    		listUserEntity = userDao.findAll();
			    	}
			    	catch (SQLException e) 
			    	{
			    		logger.error(USERSERVICEIMPL_ERROR + e);
			    	}
			  		
			    	final List<IUser> listUser = new ArrayList<IUser>();
			    	for (final UserEntity userEntity : listUserEntity) 
			    	{
			    		listUser.add(convertToModel(userEntity));
			    	}
	  		
			    	return listUser;
				}
			}, ConnectionBehavior.CLOSE);
	}

	@Override
	public void deleteUser(final int id) 
	{
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final UserDao userDao = manager.getDao(UserDao.class);
					    	try
					    	{
					    		userDao.delete(id);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(USERSERVICEIMPL_ERROR + e);
					    	} catch (Exception e) {
					    		logger.error(USERSERVICEIMPL_ERROR + e);
							}
					    	
					    	return null;
						}
					}, ConnectionBehavior.CLOSE);
	}

	@Override
	public void insertUser(final String username,final String password, final String name, final String surname)
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final UserDao userDao = manager.getDao(UserDao.class);
		    	try
		    	{
		    		userDao.insert(new UserEntity(username, password, name, surname, 0, false));
		    	}
		    	catch (Exception e) 
		    	{
		    		logger.error(USERSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);

	}

	public void changePassword(final String newPassword, final int id)
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final UserDao userDao = manager.getDao(UserDao.class);
		    	try
		    	{
		    		userDao.changePassword(newPassword, id);
		    	}
		    	catch (Exception e) 
		    	{
		    		logger.error(USERSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);


	}

	public void changeRole(final boolean role, final int id)
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final UserDao userDao = manager.getDao(UserDao.class);
		    	try
		    	{
		    		userDao.changeRole(role, id);
		    	}
		    	catch (Exception e) 
		    	{
		    		logger.error(USERSERVICEIMPL_ERROR + e);
		    	}
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);


	}
	
	private IUser convertToModel(UserEntity userEntity) 
	{
	IUser user = new User(
					userEntity.getUsername(),
					userEntity.getPassword(),
					userEntity.getName(),
					userEntity.getSurname(),
					userEntity.getId(),
					userEntity.getAdmin());
	return user;
	}
}